package test.test2;


import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Description
 * @Author Firenut
 * @Date 2023-08-21 14:09
 */
public class cacheDemo {
    static ScheduledExecutorService scheduleExecutorService = Executors.newScheduledThreadPool(3);

    public static void main(String[] args) {
        LRU lru = new LRU(5);

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Iterator<ListNode> iterator = lru.list.iterator();
                while (iterator.hasNext()) {
                    ListNode next = iterator.next();
                    if (isExpire(next)) {
                        lru.removeOldest();
                    }
                }
            }
        };

        scheduleExecutorService.scheduleAtFixedRate(timerTask, 0L, 5L, TimeUnit.SECONDS);

    }

    static boolean isExpire(ListNode node) {
        if ((System.currentTimeMillis() - node.time) / 1000 > 3) {
            return true;
        }
        return false;
    }
}


class LRU{
    ConcurrentHashMap<Integer, ListNode> map = new ConcurrentHashMap<>();
    ConcurrentLinkedQueue<ListNode> list = new ConcurrentLinkedQueue<>();
    ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    Lock readLock = reentrantReadWriteLock.readLock();
    Lock writeLock = reentrantReadWriteLock.writeLock();

    int capacity;
    public LRU(int capacity) {
        this.capacity = capacity;
    }

    Object get(int id) {
        ListNode node = map.get(id);
        return node.obj;
    }

    void set(int id,Object obj) {

    }

    void moveToTail() {

    }

    void removeOldest() {


    }

}

class ListNode {
    int id;
    Object obj;
    long time;

    public ListNode(int id, Object obj) {
        this.id = id;
        this.obj = obj;
        this.time = System.currentTimeMillis();
    }
}